home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / misc / pclta-1.000 / pclta-1 / hostappl / ha.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  8.0 KB  |  216 lines

  1. /*
  2.  * ha.c,v 1.1 1996/05/27 16:04:38 miksic Exp
  3.  */
  4.  
  5. /* HA.C -- Example LONWORKS Host Application.
  6.  * Copyright (c) 1993 by Echelon Corporation.
  7.  * All Rights Reserved.
  8.  *
  9.  * Main program for an example LONWORKS host application.  The example
  10.  * sends and receives both polls and updates to network variables.  The
  11.  * host application node declares and implements eight network variables.
  12.  * The example also processes incoming network management commands for
  13.  * network variable binding so that network variables may be bound by a
  14.  * network management tool.
  15.  *
  16.  * The example uses a LONWORKS standard network driver to communicate with
  17.  * a LONWORKS network interface such as the Echelon Serial LONTALK Adapter
  18.  * (SLTA) or a Microprocessor Interface Program (MIP)-based network interface.
  19.  *
  20.  * The example implements an application node that will be installed by
  21.  * another network management tool.
  22.  */
  23.  
  24. /****************************** Include Files *******************************/
  25.  
  26. #include "ni_msg.h"                 /* Network interface data structures    */
  27. #include "ni_mgmt.h"                /* Network management data structures   */
  28. #include "hauif.h"                  /* User interface prototypes            */
  29. #include <stdio.h>                  /* For printf()                         */
  30. #include <stdlib.h>                 /* for random()             */
  31. #include <time.h>                   /* for time( )                          */
  32. #include "conio.h"                  /* for kbhit( )         */
  33. #include <ctype.h>                  /* For toupper()                        */
  34. char            * lon_device_name = "/dev/pclta";
  35.  
  36. /*************************** Function Prototypes ****************************/
  37.  
  38.  
  39. extern boolean process_msg( ServiceType, RcvAddrDtl *,
  40.     MsgData * in_data, int in_length, boolean in_auth );     // in APPLMSG.C
  41.  
  42. extern void process_cmd( void );             /* Process keyboard command */
  43. extern void error_exit( void );
  44. extern void sign_on( void );                 // in HAUIF.C
  45. extern void load_NV_config( void );          // in APPLMSG.C
  46. void install_prog_ID( void );                // in HA.C
  47.  
  48. /*
  49.  ****************************************************************************
  50.  * main().  Display sign-on message, open network driver, and enter
  51.  *          the main scheduler loop.
  52.  ****************************************************************************
  53.  */
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57.     NI_Code         ni_error;
  58.     ServiceType     service;        /* ACKD, UNACKD_RPT, UNACKD, REQUEST */
  59.     RcvAddrDtl      in_addr;        /* address of incoming msg */
  60.     boolean         in_auth;        /* if incoming was authenticated */
  61.     MsgData         in_data;        /* data of incoming msg */
  62.     int             in_length;      /* length of incoming msg */
  63.     boolean         prompt_flag;
  64.     const boolean   true = TRUE;    /* Avoid compiler warning */
  65.     const char * prompt  = "HA> ";
  66.  
  67.     /* for terminal */
  68.     get_normal_terms();
  69.     atexit( go_normal_terms );
  70.  
  71.     /* unbuffered output */
  72.     setvbuf( stdout, NULL, _IONBF, 0 );
  73.  
  74.     /* Process command line arguments. */
  75.     while( --argc ) {
  76.         if( ( argv[ argc ][ 0 ] == '/' ) || (argv [ argc ][ 0 ] == '-') ) {
  77.             switch( toupper( argv[ argc ][ 1 ] ) ) {
  78.             case 'D':
  79.                 /* Device name */
  80.                 if( argv[ argc ][ 2 ] ) {
  81.                     /* Device name specified, copy it */
  82.                     lon_device_name = &argv[ argc ][ 2 ];
  83.                 } else error_exit( );
  84.                 break;
  85.  
  86.             case 'V':
  87.                 /* Set verbose mode */
  88.                 verbose_flag = TRUE;
  89.                 break;
  90.  
  91.             default:
  92.                 /* Invalid flag */
  93.                 error_exit( );
  94.             }
  95.         } else error_exit( );
  96.     }
  97.  
  98.     sign_on( );     // print sign on message
  99.  
  100.     /* Open and initialize the network interface. */
  101.     if( ( ni_error = ni_init( lon_device_name ) ) != NI_OK ) {
  102.         ni_error_display( "Error while initializing network interface",
  103.             ni_error );
  104.         return( 1 );
  105.     }
  106.  
  107.     load_NV_config( );      // load network variable config table from disk
  108.     install_prog_ID( );     // install program ID into network interface
  109.     printf( prompt );
  110.     while( true ) {         // main processing loop
  111.  
  112.         if( kbhit( ) ) {       // Check for keyboard activity
  113.             process_cmd( );    /* handle keyboard input */
  114.             printf( prompt );
  115.         }
  116.  
  117.         ni_error = ni_receive_msg(     // Check for network activity
  118.             & service,        /* handle incoming message to this node */
  119.             & in_addr,
  120.             & in_data,
  121.             & in_length,
  122.             & in_auth );
  123.  
  124.         prompt_flag = FALSE;
  125.         if( ni_error == NI_UPLINK_CMD ) prompt_flag = TRUE;
  126.         else if( ni_error == NI_OK )
  127.             prompt_flag = process_msg( service, & in_addr,
  128.                 & in_data, in_length, in_auth );
  129.  
  130.         if( prompt_flag ) printf( prompt );
  131.  
  132.     }           /* never leave this loop */
  133.     return 0;
  134. }
  135.  
  136. /*
  137.  ****************************************************************************
  138.  * NV_table() - display node's NV config table
  139.  ****************************************************************************
  140.  */
  141.  
  142. void NV_table( void ) {
  143.  
  144.     extern const char * svc_table[ ];       // strings for service type
  145.     extern const int num_nvs;               // in APPLMSG.C
  146.     extern nv_struct nv_config_table[ ];    // network variable config table
  147.     extern network_variable nv_value_table[ ];  // NV value table
  148.  
  149.     nv_struct        * nv_config_ptr;
  150.     network_variable * nv_value_ptr;
  151.     int                nv_index;
  152.  
  153.     printf( "Index\tSelctr\tDir\tPrio\tAuth\tAddridx\tService\tTrnArnd\n" );
  154.  
  155.     nv_config_ptr = nv_config_table;
  156.     nv_value_ptr = nv_value_table;
  157.  
  158.     for( nv_index = 0; nv_index < num_nvs; nv_index++ ) {
  159.  
  160.         printf( "%d\t", nv_index );
  161.         printf( "%2.2x%2.2x\t", nv_config_ptr->selector_hi,
  162.             nv_config_ptr->selector_lo );
  163.         printf( nv_config_ptr->direction ? "out\t" : "in\t" );
  164.         printf( nv_config_ptr->priority ? "yes\t" : "no\t" );
  165.         printf( nv_config_ptr->auth ? "yes\t" : "no\t" );
  166.         printf( "%d\t", nv_config_ptr->addr_index );
  167.         printf( "%s\t", svc_table[ nv_config_ptr->service ] );
  168.         printf( nv_config_ptr->turnaround ? "yes  " : "no   " );
  169.         printf( "%s\n", nv_value_ptr->name );
  170.  
  171.         nv_config_ptr++;       // go to next
  172.         nv_value_ptr++;
  173.     }
  174. }
  175.  
  176. /*
  177.  ****************************************************************************
  178.  * install_prog_ID - install a program ID into the network interface
  179.  ****************************************************************************
  180.  */
  181.  
  182. void install_prog_ID( void ) {
  183.     struct {
  184.         NM_write_memory_request     write_mem_hdr;
  185.         char     prog_ID[ ID_STR_LEN ];           // data to be written
  186.     } write_mem_msg = {
  187.          {  NM_write_memory,      // code
  188.             READ_ONLY_RELATIVE,   // mode
  189.             0x00, 0x0D,           // offset for id_string
  190.             ID_STR_LEN,           // count
  191.             BOTH_CS_RECALC },     // form
  192.             "HostAppl" };         // program ID
  193.  
  194.         ComplType       completion;
  195.         MsgData         response;
  196.         NI_Code         ni_error;
  197.         SendAddrDtl     net_intfc_addr;
  198.  
  199.         net_intfc_addr.lc.type = LOCAL;     // send it to the network intfc
  200.         ni_error = ni_send_msg_wait(
  201.             REQUEST,
  202.             & net_intfc_addr,         // out_addr
  203.             ( MsgData * ) &write_mem_msg,  // out_data
  204.             sizeof( write_mem_msg ),  // out_length
  205.             FALSE,                    // priority
  206.             FALSE,                    // out_auth
  207.             & completion,             // completion code
  208.             NULL,               // num_responses
  209.             NULL,               // in_addr
  210.             & response,         // in_data
  211.             NULL );             // in_length
  212.  
  213.     ( void ) handle_error( ni_error, completion, response.exp.code,
  214.         "updating program ID" );           // report any errors
  215. }
  216.